package com.jmb.apiplatform.controller;


import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.common.Result;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.pojo.PermissionRoleDao;
import com.jmb.apiplatform.pojo.RoleDao;
import com.jmb.apiplatform.pojo.RoleVO;
import com.jmb.apiplatform.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Transactional
@Tag(name = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Operation(summary = "新增角色")
    @PostMapping("/save")
    public Result saveRole(@RequestBody RoleDao roleDao) throws ValidException {
        roleService.saveRole(roleDao);
        return Result.success("新增成功");
    }
    @Operation(summary = "角色列表")
    @GetMapping
    public Result list(RoleVO roleVO) {
        PageResult<RoleDao> roleList = roleService.roleList(roleVO);
        return Result.success(roleList);
    }
    @Operation(summary = "查找角色")
    @GetMapping("/{id}")
    public Result findRoleId(@PathVariable Integer id){
        RoleDao roleDao = roleService.findId(id);
        return Result.success(roleDao);
    }
    @Operation(summary = "修改角色")
    @PostMapping("/modify")
    public Result edit(@RequestBody RoleDao roleDao) throws ValidException {
        roleService.editRole(roleDao);
        return Result.success("新增成功");
    }
    @Operation(summary = "删除角色")
    @GetMapping("/remove/{id}")
    public Result removeId(@PathVariable Integer id) throws ValidException {
        roleService.removeId(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "所有角色")
    @GetMapping("/all")
    public Result all(@RequestParam Integer status) throws ValidException {
        List<RoleDao> roleDao = roleService.allStatus(status);
        return Result.success(roleDao);
    }
    @Operation(summary = "角色权限")
    @GetMapping("/permission/{id}")
    public Result permissionId(@PathVariable Integer id) throws ValidException {
        log.info("接受到的id是{}",id);
        List<Integer> permission= roleService.checkpermissionId(id);
        return Result.success(permission);
    }
    @Operation(summary = "角色权限保存")
    @PostMapping("/permission/save")
    public Result saveRolePermission(@RequestBody PermissionRoleDao permissionRoleDao) throws ValidException {
        roleService.saveRolePermission(permissionRoleDao);
        return Result.success();
    }
    @Operation(summary = "角色权限删除")
    @PostMapping("/permission/remove")
    public Result removePermission(@RequestBody PermissionRoleDao permissionRoleDao) throws ValidException {
        roleService.removePermissionRole(permissionRoleDao);
        return Result.success();
    }
}
